//新增下级代理商
var lowerAgentAddController = function($scope, $http, LoginService) {
	$scope.init = function() {
		$scope.req = {};
		$scope.req.agentsId = LoginService.agentid;
		$scope.list();

		$scope.agentType = 1;
		$scope.agentName = "";
		$scope.agentCardId = "";
		$scope.companyName = "";
		$scope.companyId = "";
		$scope.phoneNum = "";
		$scope.emailStr = "";
		$scope.addDetail = "详细地址";
		$scope.loginId = "";
		$scope.pwd = "";
		$scope.isProfit = 1;
	};

	$scope.radioCheck = function(obj) {
		if (obj == 2) {
			// 个人
			$("#companyNameLi").hide();
			$("#companyIdLi").hide();
		} else {
			// 公司
			$("#companyNameLi").show();
			$("#companyIdLi").show();
		}
	};

	$scope.list = function() {
		$http.post("api/index/getCity").success(function(data) { // 绑定
			if (data.code == 1) {
				$scope.provinceList = data.result;
			}
		});
	};
	// 校验输入的登陆名是否已经存在
	$scope.checkIsIn = function() {
		$scope.req.loginId = $scope.loginId;
		// 登陆ID校验是否已经存在
		$http.post("api/lowerAgent/check", $scope.req).success(function(data) { // 绑定
			if (data.code == -2) {
				alert("错误信息为：" + data.message);
			} else if (data.code == -1) {
				// 已经存在
				alert("错误信息为：" + data.message);
				$scope.loginId = "";
			}
		});
	};

	$scope.isNull = function(val, valDetail) {
		if (val == "" || val == " " || val == undefined) {
			alert("输入的【" + valDetail + "】一栏不能为空，请重新输入");
			return false;
		} else {
			return true;
		}
	};

	$scope.createNew = function() {
		// 验证为空
		if (!$scope.isNull($scope.agentName, "负责人姓名")) {
			return;
		}
		if (!$scope.isNull($scope.agentCardId, "负责人身份证号")) {
			return;
		}
		if ($scope.agentType == 1) {
			if (!$scope.isNull($scope.companyName, "公司全称")) {
				return;
			}
			if (!$scope.isNull($scope.companyId, "公司营业执照登记号")) {
				return;
			}
		}
		if (!$scope.isNull($scope.phoneNum, "手机号")) {
			return;
		}
		if (!$scope.isNull($scope.emailStr, "邮箱")) {
			return;
		}
		if (!$scope.isNull($scope.addDetail, "所在地")) {
			return;
		}
		// 验证城市
		if ($scope.cityModel == undefined || $scope.proModel == undefined) {
			alert("请选择你所在的省市！");
			return;
		}
		if (!$scope.isNull($scope.loginId, "登陆ID")) {
			return;
		}

		if (!$scope.isNull($scope.pwd, "密码")) {
			return;
		}

		// 校验手机号码
		if (!checkPhone($scope.phoneNum)) {
			return;
		}

		// 校验邮箱
		if (!checkEmail($scope.emailStr)) {
			return;
		}

		// 校验身份证号
		var cardIdTemp = $scope.agentCardId;
		if (cardIdTemp.length != 18) {
			alert("输入的身份证号码长度必须为18位,请重新输入！");
			return;
		}
		// 校验密码位数大于6
		if ($scope.pwd.length < 6) {
			alert("请至少输入6位数密码");
			return;
		}

		// 校验两次输入的密码是否一致
		if ($scope.pwd != $scope.pwd1) {
			alert("两次输入的密码不一致，请重新输入");
			$scope.pwd = "";
			$scope.pwd1 = "";
			return;
		}

		$scope.req.loginId = $scope.loginId;
		$scope.req.agentType = $scope.agentType;
		$scope.req.agentName = $scope.agentName;
		$scope.req.agentCardId = $scope.agentCardId;
		$scope.req.companyName = $scope.companyName;
		$scope.req.companyId = $scope.companyId;
		$scope.req.phoneNum = $scope.phoneNum;
		$scope.req.emailStr = $scope.emailStr;
		$scope.req.addressStr = $scope.addDetail;
		$scope.req.loginId = $scope.loginId;
		$scope.req.pwd = $scope.pwd;
		$scope.req.pwd1 = $scope.pwd1;
		$scope.req.isProfit = $scope.isProfit;
		$scope.req.cityId = $scope.cityModel.id;

		$http.post("api/lowerAgent/createNew", $scope.req).success(function(data) { // 绑定
			if (data.code == -1) {
				alert("新增下级代理商失败！错误信息为：" + data.message);
			} else {
				window.location.href = "#/lowerAgent";
			}
		});
	};

	$scope.init();

};